package com.tdam2012.grupo04;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.TimeZone;

import com.tdam2012.grupo04.data.ActividadesProvider;

import android.os.Bundle;
import android.app.Activity;
import android.content.ContentValues;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

public class MailActivity extends Activity implements OnClickListener {
    private EditText etMensajeMail;
    private EditText etAsunto;
    private EditText etPara;
    private String idC;
    private Contact contacto;
    
    private static final String zonaHoraria = "GMT-03:00";
    private static final String formatoFechaHora = "dd/MM/yyyy HH:mm:ss";
    private static final String emailEnviado = "Email Enviado";
    
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        setContentView(R.layout.activity_mail);
        
        Button btnEnviar = (Button)findViewById(R.id.btnEnviarMail);
        btnEnviar.setOnClickListener(this);
        
        etMensajeMail = (EditText) findViewById(R.id.etMensajeMail);
        etAsunto = (EditText) findViewById(R.id.etAsunto);
        etPara = (EditText) findViewById(R.id.etPara);
        
        loadData();
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
       return true;
    }
    
    private void loadData()
    {
    	contacto =(Contact) getIntent().getSerializableExtra(EmailListActivity.KEY_CONTACT);
    	String email =(String) getIntent().getStringExtra(EmailListActivity.KEY_CONTACT_EMAIL);
    	etPara.setText(email);
    	idC = contacto.get_id();
    }
    
    private void enviarMail()
    {
    	Intent itSend = new Intent(android.content.Intent.ACTION_SEND);
    	itSend.setType("text/plain");
    	itSend.putExtra(android.content.Intent.EXTRA_EMAIL, etPara.getText().toString());
    	itSend.putExtra(android.content.Intent.EXTRA_SUBJECT, etAsunto.getText().toString());
    	itSend.putExtra(android.content.Intent.EXTRA_TEXT, etMensajeMail.getText());
    	
    	startActivity(Intent.createChooser(itSend, "Enviar via"));
        
        
        
        try {
        	Calendar c = Calendar.getInstance(TimeZone.getTimeZone(zonaHoraria)); 
        	SimpleDateFormat postFormater = new SimpleDateFormat(formatoFechaHora);
   	         		
    		ContentValues cv2 = new ContentValues();				
    		cv2.put(ActividadesProvider.COL_TIPO, emailEnviado);
    		cv2.put(ActividadesProvider.COL_ID, idC);
    		cv2.put(ActividadesProvider.COL_FECHA, postFormater.format(c.getTime())+" ");

    		getContentResolver().insert(ActividadesProvider.CONTENT_URI, cv2);
    		
    		Toast.makeText(MailActivity.this, getString(R.string.mail_EmailGuardado) , Toast.LENGTH_LONG).show();
		} catch (Exception e) {
			Toast.makeText(MailActivity.this, getString(R.string.mail_Error) +e.toString(), Toast.LENGTH_LONG).show();
		}
    	
    }
    
    @Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btnEnviarMail:
			enviarMail();
			break;

		default:
			break;
		}
		
	}
    
}
